<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RBISUtility
 *
 * @author DELL
 */
class RBISUtility {
    public static function encryptURLParamValue($plainParamValue)
    {
        return htmlspecialchars( base64_encode ( $plainParamValue ) );
    }

    public static function decryptURLParamValue($encryptedParamValue)
    {
        return base64_decode ( htmlspecialchars_decode ( $encryptedParamValue ) );
    }

    public static function sendEmail($to, $subject, $body, $currentUserId)
    {
        $from = 'rbis.kaust@gmail.com';
        $_flag = false;
        try
        {
            //$mail = new PHPGMailer();
            //$mail->Username = 'rbis.kaust@gmail.com';
            //$mail->Password = sfConfig::get('app_email_password'); // 'redseakaust';
            //$mail->From = 'rbis.kaust@gmail.com';
            //$mail->FromName = 'rbis.kaust';
            //$mail->Subject = 'Test Email';
            //$mail->AddAddress('rbis.kaust@gmail.com');
            //$mail->Body = 'This is a test email';
            //$mail->Send();
            $message = sfContext::getInstance()->getMailer()->compose($from, $to, $subject, $body);
            $message->setContentType('text/html');
            sfContext::getInstance()->getMailer()->send($message);

            // If email sent successfully then create log entry in the system
            Emaillog::addLog($to, $from, $subject, $body, $currentUserId);

            $_flag = true;
        }
        catch(sfException $ex)
        {
            sfContext::getInstance()->getLogger()->debug($ex->getMessage());
            $_flag = false;
        }
        return $_flag;
    }

    public static function stripText($text)
    {
        $text = strtolower($text);

        // strip all non word chars
        $text = preg_replace('/\W/', ' ', $text);

        // replace all white space sections with a dash
        $text = preg_replace('/\ +/', '-', $text);

        // trim dashes
        $text = preg_replace('/\-$/', '', $text);
        $text = preg_replace('/^\-/', '', $text);

        return $text;
    }

    public static function encryptString($string, $key)
    {
        $result = '';
        for($i=0; $i<strlen($string); $i++) {
            $char = substr($string, $i, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)+ord($keychar));
            $result.=$char;
        }

        return base64_encode($result);
    }

    public static function decryptString($string, $key)
    {
        $result = '';
        $string = base64_decode($string);

        for($i=0; $i<strlen($string); $i++) {
            $char = substr($string, $i, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)-ord($keychar));
            $result.=$char;
        }
        return $result;
    }

    //http://php.net/manual/en/function.crypt.php
    public static function RIJNDAEL_encrypt($text){

        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $key = sfConfig::get('app_Password_key');
        return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $text, MCRYPT_MODE_ECB, $iv));

    }

    public static function RIJNDAEL_decrypt($text){

        $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB);
        $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
        $key = sfConfig::get('app_Password_key');
      //I used trim to remove trailing spaces
        return trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, base64_decode($text), MCRYPT_MODE_ECB, $iv));

    }

    /*
     * Converts Bytes to corresponding size
     */
    public static function byteToSize($bytes)
    {
        $size = $bytes / 1024;

        if($size < 1024)
        {
            $size = number_format($size, 2);
            $size .= ' KB';
        }
        else
        {
            if($size / 1024 < 1024)
            {
                $size = number_format($size / 1024, 2);
                $size .= ' MB';
            }
            elseif ($size / 1024 / 1024 < 1024)
            {
                $size = number_format($size / 1024 / 1024, 2);
                $size .= ' GB';
            }
        }
        return $size;
    }
}
?>
